//
//  FeedBackHandle.m
//  UILbrary
//
//  Created by Apple on 2023/3/2.
//  Copyright © 2023 mac. All rights reserved.
//

#import "JXFeedBackHandle.h"
#import "JXFeedBackSelectView.h"
#import "HSFInputFieldView.h"
#import "WBImgListHandle.h"
#import "FBContactInfoHandle.h"
#import "FBReportHandle.h"
#import "FBSubmitHandle.h"
#import "FBInputContentHandle.h"
#import "JXFeedBackConfigModel.h"



@interface JXFeedBackHandle()

@property (nonatomic, strong) UIView *p_view;
@property (nonatomic, strong) UIScrollView *scrollview;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) JXFeedBackSelectView *selectView;
@property (nonatomic, strong) FBInputContentHandle *inputContentHandle;
@property (nonatomic, strong) WBImgListHandle *imgListHandle;
@property(nonatomic, strong) FBContactInfoHandle *contactInfoHandle;
@property(nonatomic, strong) FBReportHandle *reportHandle;
@property(nonatomic, strong) FBSubmitHandle *submitHandle;

@end

@implementation JXFeedBackHandle(DataTransform)

- (NSArray <JXSecondHandDropViewSelectModel *>*)listFromlist:(NSArray <JXFeedBackSelectItem *> *)orignList
{
    if(!orignList) return @[];
    if(orignList.count <= 0) return @[];
    
    NSMutableArray *arr = [@[] mutableCopy];
    [orignList enumerateObjectsUsingBlock:^(JXFeedBackSelectItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        JXSecondHandDropViewSelectModel *result = [JXSecondHandDropViewSelectModel new];
        result.isSelect = obj.select;
        result.title = obj.title;
        result.idx = obj.idx;
        [arr addObject:result];
    }];
    
    return [arr copy];
}

@end

@implementation JXFeedBackHandle


//MARK: - system

- (instancetype)init
{
    self = [super init];
    if(self){
        [self p_initConfig];
    }
    return self;
}


//MARK: - public

- (void)configWithData:(JXFeedBackConfigModel *)data
{
    NSArray <JXSecondHandDropViewSelectModel *>* selectArr = [self listFromlist:data.selectList];
    [self.selectView configWithDatas:selectArr];
}

//MARK: - private

- (void)p_initConfig
{
    [self p_configSubviews];
    self.selectView.nextStep = self.inputContentHandle;
    self.inputContentHandle.nextStep = self.imgListHandle;
    self.imgListHandle.nextStep = self.contactInfoHandle;
    self.contactInfoHandle.nextStep = self.submitHandle;
}


- (void)p_configSubviews
{
    [self.view addSubview:self.scrollview];
    [self.scrollview addSubview:self.contentView];
    
    
    [self.contentView addSubview:self.selectView];
    [self.contentView addSubview:self.inputContentHandle.view];
    [self.contentView addSubview:self.imgListHandle.view];
    [self.contentView addSubview:self.contactInfoHandle.view];
    [self.contentView addSubview:self.reportHandle.view];
    [self.contentView addSubview:self.submitHandle.view];
    
    // Calculate the content size of the scroll view based on the subviews in the contentView
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollview);
        make.width.equalTo(self.scrollview.mas_width);
        make.bottom.mas_equalTo(self.submitHandle.view);
    }];
    
    [self.scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
        make.leading.equalTo(self.view.mas_leading);
        make.trailing.equalTo(self.view.mas_trailing);
        make.width.lessThanOrEqualTo(self.view.mas_width);
        make.height.lessThanOrEqualTo(self.view.mas_height);
        // Set the content size of the scrollview to be equal to the size of the contentView when the scrollview is less than the controller view
        make.height.equalTo(self.contentView.mas_height).priorityLow();
    }];
    
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView.mas_topMargin).offset(Space_middle);
    }];
    
    [self.inputContentHandle.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.selectView.mas_bottom);
        make.right.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView);
        make.height.mas_equalTo(150);
    }];
    
    [self.imgListHandle.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.inputContentHandle.view.mas_bottom);
    }];
    
    [self.contactInfoHandle.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.imgListHandle.view.mas_bottom).offset(Space_middle);
        make.height.mas_equalTo(45);
    }];
    
    [self.reportHandle.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contactInfoHandle.view.mas_bottom).offset(Space_middle);
        make.height.mas_equalTo(45);
    }];
    
    [self.submitHandle.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.reportHandle.view.mas_bottom).offset(50);
        make.height.mas_equalTo(40);
    }];
    
}

//MARK: - get

- (UIView *)p_view
{
    if(!_p_view){
        _p_view = [UIView new];
        _p_view.backgroundColor = JX_View_BackgroudColor;
    }
    return _p_view;
}

- (UIView *)contentView
{
    if(!_contentView){
        _contentView = [UIView new];
        _contentView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _contentView;
}

- (UIScrollView *)scrollview
{
    if(!_scrollview){
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.showsVerticalScrollIndicator = YES;
        scrollView.translatesAutoresizingMaskIntoConstraints = NO;
        _scrollview = scrollView;
    }
    return _scrollview;
}

- (UIView *)view
{
    return self.p_view;
}

- (JXFeedBackSelectView *)selectView
{
    if(!_selectView){
        _selectView = [JXFeedBackSelectView new];
        _selectView.backgroundColor = [UIColor whiteColor];
        [_selectView initConfig];
    }
    return _selectView;
}

- (FBInputContentHandle *)inputContentHandle
{
    if(!_inputContentHandle){
        _inputContentHandle = [FBInputContentHandle new];
    }
    return _inputContentHandle;
}

- (WBImgListHandle *)imgListHandle
{
    if(!_imgListHandle){
        _imgListHandle = [WBImgListHandle new];
        _imgListHandle.view.backgroundColor = [UIColor whiteColor];
    }
    return _imgListHandle;
}

- (FBContactInfoHandle *)contactInfoHandle
{
    if(!_contactInfoHandle){
        _contactInfoHandle = [FBContactInfoHandle new];
        _contactInfoHandle.view.backgroundColor = [UIColor whiteColor];
    }
    return _contactInfoHandle;
}

- (FBReportHandle *)reportHandle
{
    if(!_reportHandle){
        _reportHandle = [FBReportHandle new];
        _reportHandle.view.backgroundColor = [UIColor whiteColor];
    }
    return _reportHandle;
}

- (FBSubmitHandle *)submitHandle
{
    if(!_submitHandle){
        _submitHandle = [FBSubmitHandle new];
        _submitHandle.checkHandle = self.selectView;
    }
    return _submitHandle;
}
@end
